package ar.republicagroup.gestion.util.helpers;

import ar.republicagroup.gestion.config.RGroupAppConfig;
import ar.republicagroup.gestion.entities.CalendarioBancario;
import ar.republicagroup.gestion.util.JComboBoxModel;
import com.toedter.calendar.JCalendar;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author DIEGOALV
 */
public class UserInterfaceHelper {
    
    public static boolean BY_VALUE = true;
    public static boolean BY_LABEL = false;
    
    
    /**
     * Este método ayuda a las vistas a convertir un model(Vector<Vector>) en un List<> de un objecto en particular
     *@param data es el Vector<Vector> que viene de obtener un model de un JTable
     *@param objectIndex Es el indice donde se encuentra el objecto en en el vector del cual queremos armar un List<> 
     */
    public static List<Object> convertVectorToList(Vector<Vector> data,int objectIndex){
    
        List<Object> resultList = new ArrayList<>();
        
        
        for (int i = 0; i < data.size(); i++) {
            resultList.add(data.get(i).elementAt(objectIndex));
        }
        
        return resultList;
    }
    
    public static void centreWindow(Window frame) {
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }
    
    
    /* Actualiza un JComboBox dejando únicamente los elementos que estén en el idsToSelectList */
    public static void refreshJComboBoxWithThisList(List<String> toSelectList,JComboBox comboToRefresh,boolean isByValue){
        DefaultComboBoxModel model  = (DefaultComboBoxModel)comboToRefresh.getModel();
        
        DefaultComboBoxModel nuevoModel = new DefaultComboBoxModel<>();
        nuevoModel.addElement("Seleccione");
        
        for(String idToKeepInCombo : toSelectList){
            for (int i = 0 ; i < comboToRefresh.getItemCount(); i++){
                JComboBoxModel myRow = (JComboBoxModel)model.getElementAt(i);
              if(isByValue){
                    if(myRow.getValue().equals(idToKeepInCombo)){
                        JComboBoxModel nuevoItem = new JComboBoxModel(myRow.getValue(), myRow.getLabel());
                        nuevoModel.addElement(nuevoItem);
                    }
              }else {
                   if(myRow.getLabel().equals(idToKeepInCombo)){
                      JComboBoxModel nuevoItem = new JComboBoxModel(myRow.getValue(), myRow.getLabel());
                      nuevoModel.addElement(nuevoItem);
                   }
              }
            }
        }
        comboToRefresh.setModel(nuevoModel);
    }
    
    public static void setJFrameIconImage(JFrame jframe){
        jframe.setIconImage(new ImageIcon(jframe.getClass().getResource(RGroupAppConfig.getProperty("iconForFramesPath"))).getImage());
    }
    
    
    public static void setTitleToThisJFrame(JFrame jframe,String title){
        jframe.setTitle(title);
    }

    public static String getSelectedButtonText(ButtonGroup buttonGroup) {
        
        for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                return button.getText();
            }
        }

        return null;
    }
    
    public static void drawHolidaysOnThisCalendar(JCalendar myCalendar , Set<CalendarioBancario> holidays){
        Calendar cal = Calendar.getInstance();
        cal.setTime(myCalendar.getDate());
        int currentMonth = cal.get(Calendar.MONTH);
        int currentYear = cal.get(Calendar.YEAR);
       
        JPanel jPanel = myCalendar.getDayChooser().getDayPanel();
        Component component[] = jPanel.getComponents();
         
        
            for(CalendarioBancario feriado : holidays){
                Calendar feriadito = Calendar.getInstance(); 
                feriadito.setTime(feriado.getFechaFeriado());
                
                int mesDelFeriado = feriadito.get(Calendar.MONTH);
                int anoDelFeriado = feriadito.get(Calendar.YEAR);
                
                if((currentMonth == mesDelFeriado) && (currentYear == anoDelFeriado)){
                     // Calculate the offset of the first day of the month
                    cal.set(Calendar.DAY_OF_MONTH,1);
                    int offset = cal.get(Calendar.DAY_OF_WEEK) - 1;
                    component[ feriado.getFechaFeriado().getDate() - 1  + offset + 7 ].setBackground(Color.RED); 
                }
            }
    }
    
    
    public static void setJComboBoxEnabledByCheckBoxSelected(JComboBox comboBox,JCheckBox checkBox){
        
        if( checkBox.isSelected() ) {
            comboBox.setEnabled(Boolean.TRUE);
        } else {
            comboBox.setEnabled(Boolean.FALSE);
        }

    }
    
}
